package com.lu.statistics.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.lu.statistics.common.config.ServerResponse;
import com.lu.statistics.entity.front.response.AppPermissionResponse;
import com.lu.statistics.entity.front.response.UserPermissionResponse;
import com.lu.statistics.entity.vo.UserPermissions;
import com.lu.statistics.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(value = "权限模块", tags = {"APP权限模块"})
@RestController
@RequestMapping(value = "/api/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "查询权限菜单列表", notes = "查询权限菜单列表")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", required = true, name = "type", value = "菜单类型", dataType = "String")})
    @GetMapping(value = "/queryUserPermissionList")
    private ServerResponse<List<UserPermissionResponse>> queryAppPermissionList(String type) {
        Integer userId = Integer.parseInt(StpUtil.getLoginId().toString());
        List<UserPermissions> permissionsList = permissionService.queryUserPermMenuList(userId, type);
        List<UserPermissionResponse> newList = new ArrayList<>();
        if (permissionsList != null && permissionsList.size() > 0) {
            for (int i = 0; i < permissionsList.size(); i++) {
                UserPermissions permissions = permissionsList.get(i);
                String url = "/api/file/previewPhoto?fileFullPath=" + permissions.getIcon();
                UserPermissionResponse permissionResponse = new UserPermissionResponse();
                BeanUtils.copyProperties(permissions, permissionResponse);
                permissionResponse.setId(i + 1);
                permissionResponse.setMenuId(permissions.getId());
                permissionResponse.setIcon(url);
                newList.add(permissionResponse);
            }
        }
        return ServerResponse.success(newList);
    }

    @ApiOperation(value = "查询菜单列表(访客)", notes = "查询菜单列表(访客)")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", required = true, name = "type", value = "菜单类型", dataType = "String")})
    @GetMapping(value = "/queryRolePermissionList")
    private ServerResponse<List<UserPermissionResponse>> queryRolePermissionList(String type) {
        List<UserPermissions> permissionsList = permissionService.queryRolePermMenuList("R10005", type);
        List<UserPermissionResponse> newList = new ArrayList<>();
        if (permissionsList != null && permissionsList.size() > 0) {
            for (int i = 0; i < permissionsList.size(); i++) {
                UserPermissions permissions = permissionsList.get(i);
                String url = "/api/file/previewPhoto?fileFullPath=" + permissions.getIcon();
                UserPermissionResponse permissionResponse = new UserPermissionResponse();
                BeanUtils.copyProperties(permissions, permissionResponse);
                permissionResponse.setId(i + 1);
                permissionResponse.setMenuId(permissions.getId());
                permissionResponse.setIcon(url);
                newList.add(permissionResponse);
            }
        }
        return ServerResponse.success(newList);
    }

    @ApiOperation(value = "应用权限菜单列表", notes = "应用权限菜单列表")
    @GetMapping(value = "/queryAppPermissionList")
    private ServerResponse<AppPermissionResponse> queryAppPermissionList() {
        AppPermissionResponse permissionResponse = new AppPermissionResponse();
        List<UserPermissionResponse> userPermMenus = permissionService.getUserPermMenus("R10005", "HOMEPAGE");
        permissionResponse.setHomePerms(userPermMenus);
        List<UserPermissionResponse> tool = permissionService.getUserPermMenus("R10005", "TOOL");
        permissionResponse.setToolPerms(tool);
        return ServerResponse.success(permissionResponse);
    }


}
